/**
 * 
 */
package org.huanglei.account.view;

import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;

import org.huanglei.account.MyApplication;
import org.huanglei.account.R;
import org.huanglei.account.bean.Account;
import org.huanglei.account.provider.DataProvider;
import org.huanglei.account.provider.DataProvider.StaticDateFormat;
import org.huanglei.account.provider.DataProviderFactory;
import org.huanglei.account.view.adapter.AccountListAdapter;
import org.huanglei.account.view.base.TitleBarActivity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 
 * @author huanglei
 * @data Jul 10, 2012
 * @tag 账务列表
 */
public class AccountListActivity extends TitleBarActivity implements OnItemLongClickListener{

	private DataProvider dataProvider = null;

	private final Context me = this;

	private List<Account> accounts = null;

	@Override
	protected int getContentView() {
		return R.layout.account_list;
	}

	@Override
	protected void init() {
		MyApplication.getInstance().addActivity(this);
		
		initData();
		initTitleBar();
		initView();
	}

	/**
	 * 初始化标题
	 */
	private void initTitleBar() {
		setBarTitle(getResources().getString(R.string.account_list));
		showBarView(TitleBarView.TBV_NEW, new ClickListener() {
			@Override
			public ClickListener setObject(Object object) {
				return null;
			}

			@Override
			public void onClick(TitleBarActivity activity, View view) {
				Intent intent = new Intent();
				intent.setClass(activity, AccountActivity.class);
				startActivity(intent);
			}
		});
		showBarView(TitleBarView.TBV_HOME,
				CL_HOME.setObject(DashboardActivity.class));
	}

	/**
	 * 初始化数据
	 */
	private void initData() {
		this.dataProvider = DataProviderFactory.getProvider(me,
				DataProviderFactory.LOCAL);
		accounts = dataProvider.getAccounts();
		
		// getBillsTotal
		TextView total = (TextView) findViewById(R.id.totalitem);
		total.setText(getToal());
		
	}

	/**
	 * 初始化控件及相关事件
	 */
	private void initView() {
		ListView listView = (ListView) findViewById(R.id.account_list_listview);
		listView.setAdapter(new AccountListAdapter(this, accounts));
		
		listView.setOnItemLongClickListener((OnItemLongClickListener) this);
		
	}
	
	private String getToal(){
		
		String date = getYear();
		Double sr = 0.0;
		Double zc = 0.0;
		
		Map<String, Double> map =  dataProvider.getStatistics(date, StaticDateFormat.YEAR);
		Set<String> key = map.keySet();
		for (Iterator<String> it = key.iterator(); it.hasNext();) {
			String temp = String.valueOf(it.next());
			if("工资".equals(temp)){
				sr += map.get(temp);
			}
			if(!"工资".equals(temp)){
				zc += map.get(temp);
			}
		 }
		
		String total = "总收入:" + sr + " 支出:" + zc + " 小计:" + (sr - zc);
		
		return total;
	}
	
	private String getYear(){
		
		Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
		int year = c.get(Calendar.YEAR);
		
		return String.valueOf(year);
	}

	//鼠标长按事件
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			final int position, long id) {
		
		final TextView tv = (TextView)view.findViewById(R.id.account_list_item_id);
		
		new AlertDialog.Builder(this).setTitle("提示").setMessage(
				"删除后数据无法回复，确定删除该明细?").setIcon(R.drawable.quit).setPositiveButton("确定",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						
						dataProvider.delete(Integer.valueOf(tv.getText().toString()));
					
						toBackup();
					
					}
				}).setNegativeButton("取消",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						
					}
				}).show();

		return true;
		
	}
	
	/**
	 * 删除成功 跳转数据列表
	 */
	private void toBackup(){
        Intent intent = new Intent();  
        intent.setClass(AccountListActivity.this, AccountListActivity.class);  
        startActivity(intent);
	}
	
}
